1:Intellij 新建服务 user-service
2:pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cn.tigee</groupId><artifactId>user-service</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>user-core</artifactId><properties>
<!-- <java.version>1.8</java.version>--><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>cn.tigee</groupId><artifactId>user-client</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies></project>
3:启动类
package cn.tigee;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
@EnableDiscoveryClient
public class ApplicationMain {static final Logger logger = LoggerFactory.getLogger(ApplicationMain.class);public static void main(String[] args) {logger.debug("服务启动..start......");SpringApplication.run(ApplicationMain.class, args);logger.debug("服务启动..end......");}}
4:application.xml
package cn.tigee;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
@EnableDiscoveryClient
public class ApplicationMain {static final Logger logger = LoggerFactory.getLogger(ApplicationMain.class);public static void main(String[] args) {logger.debug("服务启动..start......");SpringApplication.run(ApplicationMain.class, args);logger.debug("服务启动..end......");}}
5:restfull 接口
package cn.tigee.controller;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@Value("${server.port}")private int port;@Value("${spring.application.name}")private String serve;@RequestMapping("hello")public String hello(){return "你好:"+serve+":"+port;}
}
6:启动
7:访问 localhost:7003/hello?name=12
8:查看注册中心