上次说到Eureka服务注册发现,现在写一个服务(提供服务)向eureka注册
1.新建SpringBoot项目,导入jar包
xml version="1.0" encoding="UTF-8"?>
<project xmlns&#61;"http://maven.apache.org/POM/4.0.0" xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation&#61;"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0modelVersion><groupId>com.examplegroupId><artifactId>Server_AartifactId><version>0.0.1-SNAPSHOTversion><packaging>jarpackaging><name>Server_Aname><description>Demo project for Spring Bootdescription><parent><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-parentartifactId><version>1.5.12.RELEASEversion><relativePath/> parent><properties><project.build.sourceEncoding>UTF-8project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding><java.version>1.8java.version><spring-cloud.version>Edgware.SR3spring-cloud.version>properties><dependencies><dependency><groupId>org.springframework.cloudgroupId><artifactId>spring-cloud-starter-eurekaartifactId>dependency><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-testartifactId><scope>testscope>dependency>dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloudgroupId><artifactId>spring-cloud-dependenciesartifactId><version>${spring-cloud.version}version><type>pomtype><scope>importscope>dependency>dependencies>dependencyManagement><build><plugins><plugin><groupId>org.springframework.bootgroupId><artifactId>spring-boot-maven-pluginartifactId>plugin>plugins>build>project>
2.写一个Controller类&#xff0c;提供服务
package com.example.demo;import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;&#64;RestController
public class ComputeController {private final Logger logger &#61; Logger.getLogger(getClass());&#64;Autowiredprivate DiscoveryClient client;&#64;RequestMapping(value &#61; "/add" ,method &#61; RequestMethod.GET)public Integer add(&#64;RequestParam Integer a, &#64;RequestParam Integer b) {&#64;SuppressWarnings("deprecation")ServiceInstance instance &#61; client.getLocalServiceInstance();Integer r &#61; a &#43; b;logger.info("/add, host:" &#43; instance.getHost() &#43; ", service_id:" &#43; instance.getServiceId() &#43; ", result:" &#43; r);return r;}
}
2.启动SpringBoot
package com.example.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;&#64;SpringBootApplication
&#64;EnableDiscoveryClient
public class ServerAApplication {//&#64;EnableDiscoveryClient
注解是基于spring-cloud-commons
依赖&#xff0c;并且在classpath中实现public static void main(String[] args) {SpringApplication.run(ServerAApplication.class, args);}
}
3.最后写一个application.xml配置文件向服务注册中心注册
&#xff08;spring.application.name 服务名称&#xff0c;这里服务名称里只能用中线&#xff0c;不能用下划线&#xff09;
spring.application.name&#61;Server-Aserver.port&#61;2222eureka.client.serviceUrl.defaultZone&#61;http://localhost:1111/eureka/
4.启动项目此时&#xff0c;访问http://localhost:1111&#xff08;看到服务已经注册成功&#xff09;
完整代码&#xff1a;https://github.com/wang19941126/SpringCloud