eureka高可用相互注册
服务者
修改pom文件
org.springframework.cloud
spring-cloud-starter-eureka-server
1.4.3.RELEASE
org.springframework.boot
spring-boot-starter-security
配置文件
server.port=10000
#应为当前的eureka是单机的,所以我们需要做一些配置
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://user:123@localhost:10000/eureka
#开启安全配置,也就是需要密码,如果不需要设置false即可,注意这个参数必须放在application文件中
security.basic.enabled=true
security.user.name=user
#在配置了用户和密码,我们可以修改地址访问风格为curl风格
security.user.password=123
eureka.instance.prefer-ip-address=true
启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaApp {
public static void main(String[] args) {
SpringApplication.run(EurekaApp.class, args);
}
}
提供者,
修改pom文件
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
1.4.3.RELEASE
org.springframework.boot
spring-boot-starter-actuator
修改配置文件
server.port=7900
spring.application.name=provider-user
eureka.client.serviceUrl.defaultZone=http://user:123@localhost:10000/eureka
eureka.instance.prefer-ip-address=true
写一个controller
@RestController
public class UserController {
@GetMapping("/user/{id}")
public User getinfo(@PathVariable Long id) {
return new User(id);
}
@PostMapping("/user2")
public User getinfo(User user) {
return user;
}
}
启动类
@SpringBootApplication
@EnableEurekaClient
public class UserApp {
public static void main(String[] args) {
SpringApplication.run(UserApp.class, args);
}
}
消费者
ribbon
修改pm文件
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
1.4.3.RELEASE
org.springframework.boot
spring-boot-starter-actuator
修改配置文件
server.port=8900
spring.application.name=sp-orders
user.url=http://localhost:7900/user/
eureka.client.serviceUrl.defaultZone=http://user:123@localhost:10000/eureka
eureka.instance.prefer-ip-address=true
controller
@RestController
public class OrderController {
@Autowired
private EurekaClient eurekaClient;
@Autowired
private RestTemplate restTemplate;
@Autowired
private LoadBalancerClient loadBalancerClient;
//String url="http://localhost:7900/user/";
@Value("${user.url}")
private String url;
@GetMapping("/order/{id}")
public User getOrder(@PathVariable Long id) {
InstanceInfo inst = eurekaClient.getNextServerFromEureka("PROVIDER-USER", false);
url=inst.getHomePageUrl();
System.out.println("URL:"+url);
User user= restTemplate.getForObject(url+"/user/"+id, User.class);
return user;
}
@GetMapping("/order2/{id}")
public User getOrder2(@PathVariable Long id) {
System.out.println("getOrder2..........");
User user= restTemplate.getForObject("http://PROVIDER-USER/user/"+id, User.class);
return user;
}
@GetMapping("/test")
public String test() {
ServiceInstance serviceInstance = loadBalancerClient.choose("SP-USERS");
System.out.println("ok:"+serviceInstance.getServiceId()
+":"+serviceInstance.getHost()+":"+serviceInstance.getPort());
return "ok";
}
}
ribbon配置类,负载均衡
@Configuration
@ExculdeCommentScan
public class TestConfig {
@Autowired
private IClientConfig clientConfig;
public IRule ribbonRule(IClientConfig clientConfig) {
return new RandomRule();
}
}
注释类
public @interface ExculdeCommentScan {
}
启动类
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name="PROVIDER-USER",configuration = TestConfig.class)
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = ExculdeCommentScan.class)})
public class App {
@Bean
public RestTemplate getTemp() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
feign
修改POM文件
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
1.4.3.RELEASE
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-openfeign
1.4.3.RELEASE
配置文件
server.port=8900
spring.application.name=order-feign
eureka.client.serviceUrl.defaultZone=http://user:123@localhost:10000/eureka
eureka.instance.prefer-ip-address=true
logging.level.com.ljw.feign.client.MyFeighClient2=debug
controller
@RestController
public class OrderController {
@Autowired
private MyFeighClient myFeignClient;
@Autowired
private MyFeighClient2 myFeighClient2;
@GetMapping("/order/{id}")
public User getUser(@PathVariable Long id) {
System.out.println("...............");
User user = myFeignClient.getUser(id);
System.out.println("+++++++++++++++");
return user;
}
@GetMapping("/order2")
public User getUser(User user) {
System.out.println("...............");
User user1 = myFeignClient.getUser2(user);
System.out.println("+++++++++++++++");
return user1;
}
@RequestMapping("/info/{name}")
public String getServiceInfo(@PathVariable String name) {
System.out.println("services info............");
return myFeighClient2.getServiceInfo(name);
}
}
feignclient
@FeignClient("PROVIDER-USER")
public interface MyFeighClient {
@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
User getUser(@PathVariable(value="id") Long id);
/**
* 无法方位提供者那边必须是post方式这边才可以,如果非要使用get传多个参数,只能以普通方式传递,不能封装为复杂对象
* 如果你传递的是复杂参数,那么feign不管配置是什么请求方式,都会以post方式发出
* @param user
* @return
*/
@RequestMapping(value = "/user2",method = RequestMethod.POST)
User getUser2(User user);
}
@FeignClient(name="PROVIDER-USER",url = "http://localhost:10000",configuration = FeignConfig.class)
public interface MyFeighClient2 {
@RequestMapping(value = "/eureka/apps/{servicename}",method = RequestMethod.GET)
String getServiceInfo(@PathVariable(value="servicename") String servicename);
}
feignConfig
@Configuration
public class FeignConfig {
/**
* 创建用户和密码对象
* @return
*/
@Bean
public BasicAuthRequestInterceptor authRequestInterceptor() {
return new BasicAuthRequestInterceptor("user","123");
}
/*
* 配置输出日志是那些,必须在debug模式下才可以输出
*/
@Bean
Logger.Level feignloggerlevel(){
return Logger.Level.FULL;
}
}
启动类
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FeignApp {
public static void main(String[] args) {
SpringApplication.run(FeignApp.class, args);
}
}