标签:client adb mic pre balancer print localhost omr vax
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--eureka依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependencyManagement> <dependencies> <!--springCloud依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
##应用名称 spring: application: name: eureka-provider ##声明当前eurekaservice的端口号 server: port: 8890 ##配置eureka eureka: client: service-url: defaultZone: http://localhost:8888/eureka
package com.zn.service; public interface IDoSomeService { public String doSome(); }
package com.zn.service.impl; import com.zn.service.IDoSomeService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class IDoSomeServiceImpl implements IDoSomeService { @RequestMapping("/doSome") @Override public String doSome() { System.out.println("服务提供者!"); return "eureka到达页面hhhh"; } }
<!--eureka依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependencyManagement> <dependencies> <!--springCloud依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
##应用名称 spring: application: name: eureka-consumer ##声明当前eurekaservice的端口号 server: port: 8891 ##配置eureka eureka: client: service-url: defaultZone: http://localhost:8888/eureka
package com.zn.controller; import com.zn.service.IDoSomeService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @RestController public class IDoSomeController { @Resource RestTemplate restTemplate; /*@Resource IDoSomeService iDoSomeService; */ @RequestMapping("/doSome") public String doSome(){ System.out.println("ConsumerController"); return restTemplate.getForObject("http://eureka-provider/doSome",String.class); //return iDoSomeService.doSome(); } }
package com.zn; import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.RandomRule; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication public class StartEurekaConsumer { public static void main(String[] args) { SpringApplication.run(StartEurekaConsumer.class,args); } @Bean @LoadBalanced //实现负载均衡 RestTemplate restTemplate(){ return new RestTemplate(); } }
<!--feign依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
(2)IDoSomeService
package com.zn.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; @FeignClient("eureka-provider") public interface IDoSomeService { @RequestMapping("/doSome") public String doSome(); }
package com.zn.controller; import com.zn.service.IDoSomeService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @RestController public class IDoSomeController { //@Resource //RestTemplate restTemplate; @Resource IDoSomeService iDoSomeService; @RequestMapping("/doSome") public String doSome(){ System.out.println("ConsumerController"); //return restTemplate.getForObject("http://eureka-provider/doSome",String.class); return iDoSomeService.doSome(); } }
SpringCloud微服务实现生产者消费者+ribbon负载均衡
标签:client adb mic pre balancer print localhost omr vax
原文地址:https://www.cnblogs.com/Zzzzn/p/12055571.html