smilodon中文是剑齿虎,是一套服务注册发现工具包,实现的目的是更好了解微服务的原理,实现
spring-cloud-commons
部分功能,支持@LoadBalanced
注解,可以使用RestTemplate调用服务
- 先决条件
- JDK 1.8或更高
- springboot 2.3或更高
- springcloud Hoxton.SR8或更高
- maven 依赖
<dependency> <groupId>cn.lilq.smilodon</groupId> <artifactId>smilodon-server-spring-boot-starter</artifactId> <version>0.2.4</version> </dependency>
- 启用smilodon服务器
@EnableSmilodonServer @SpringBootApplication public class SmilodonServerApplication { public static void main(String[] args) { SpringApplication.run(SmilodonServerApplication.class,args); } }
- 可选配置
smilodon.instance.prefer-ip-address=true smilodon.instance.hostname=127.0.0.1 smilodon.server.max-wait-time=60 smilodon.server.testing-time=20
- 先决条件
- JDK 1.8或更高
- springboot 2.3或更高
- springcloud Hoxton.SR8或更高
- maven 依赖
<dependency> <groupId>cn.lilq.smilodon</groupId> <artifactId>smilodon-client-spring-boot-starter</artifactId> <version>0.2.4</version> </dependency>
- 启用smilodon服务器
@EnableSmilodonClient @SpringBootApplication public class SmilodonClientApplication { public static void main(String[] args) { SpringApplication.run(SmilodonClientApplication.class,args); } }
- 可选配置
smilodon.instance.prefer-ip-address=true smilodon.instance.instance-id=ordersys5001 smilodon.client.fetch-registry=false #smilodon.client.fetch-registry=true smilodon.client.service-url=http://localhost:7010 #smilodon.client.register-with-smilodon=false smilodon.client.register-with-smilodon=true
- 使用
discoveryClient
进行服务发现@Resource private DiscoveryClient discoveryClient; @ResponseBody @RequestMapping(value = "/",method = RequestMethod.GET) public void getServices(){ System.out.println(discoveryClient.getServices()); } @ResponseBody @RequestMapping(value = "/{id}",method = RequestMethod.GET) public void getInstances(@PathVariable String id){ System.out.println(discoveryClient.getInstances(id)); }
- 使用具有
@LoadBalanced
注解的RestTemplate调用服务@ResponseBody @RequestMapping(value = "/hello", method = RequestMethod.GET) public Response hello() { return new Response(200,"successful","hello world,port:"+serverPort); } @ResponseBody @RequestMapping(value = "/sayhello", method = RequestMethod.GET) public Response sayHello() { Response response = restTemplate.getForObject("http://ORDER-SERVER/hello", Response.class); System.out.println(response); return response; } @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); }
- 动态注入controller
- 心跳检测
- 订阅controller 动态注入 (注入异常)